package com.wty.cmims.controller;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.alipay.easysdk.factory.Factory;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wty.cmims.config.AliPayConfig;
import com.wty.cmims.pojo.AliPay;
import com.wty.cmims.pojo.Appointment;
import com.wty.cmims.pojo.Doctor;
import com.wty.cmims.pojo.Orders;
import com.wty.cmims.pojo.ResponseResult;
import com.wty.cmims.service.AliPayService;
import com.wty.cmims.service.AppointmentService;
import com.wty.cmims.service.DoctorService;
import com.wty.cmims.service.OrdersService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("alipay")
public class AliPayController {
   @Resource
   private AliPayService aliPayService;
   /**
    * 处理支付的逻辑
    * @param aliPay
    * @param httpResponse
    */
   @GetMapping("/pay") // 前端路径参数格式?subject=xxx&tradeNo=xxx&totalAmount=xxx
   public void pay(@Validated AliPay aliPay, HttpServletResponse httpResponse){
      aliPayService.pay(aliPay, httpResponse);
   }

   /**
    * 处理支付成功的回调
    * @param request
    * @return
    */
   @PostMapping("/notify")  // 注意这里必须是POST接口
   public String payNotify(HttpServletRequest request){
      return aliPayService.payNotify(request);
   }

   /**
    * 处理退款
    * @param tradeNo
    * @return
    */
   @GetMapping("/refund")
   public ResponseResult<Map<String,Object>> refund(String tradeNo){
      return aliPayService.refund(tradeNo);
   }
}